package com.zsummer.zero.hibernate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

/**
 * hibernate base crud
 * get方法与load方法的区别：load方法是延时加载，当需要使用查询对象时才执行查询sql
 * @author zhouyujiang
 *
 */
public class Demo1 {

	public static void main(String[] args) {
		
		//run1();
		//run2();
		//run3();
		//run4();
		//run5();
		run6();
	}
	
	/**
	 * select all
	 */
	private static void run6() {
		Configuration configure = new Configuration().configure();
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		
		@SuppressWarnings("unchecked")
		Query<Person> query = session.createQuery("from Person");
		List<Person> list = query.list();
		System.out.println(list);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * delete
	 */
	private static void run5() {
		Configuration configure = new Configuration().configure();
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		
		//Person person = session.get(Person.class, 3);
		Person person = session.load(Person.class, 3);
		session.delete(person);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * update
	 */
	private static void run4() {
		Configuration configure = new Configuration().configure();
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		
		//Person person = session.get(Person.class, 3);
		Person person = session.load(Person.class, 3);
		person.setAge((short) 100);
		person.setName("大锤");
		session.update(person);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}

	/**
	 * select
	 */
	private static void run3() {
		Configuration configure = new Configuration().configure();
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.getTransaction().begin();
		
		Person person = session.get(Person.class, 3);
		//Person person = session.load(Person.class, 3);
		System.out.println(person);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		
	}
	/**
	 * insert
	 */
	private static void run2() {
		
		Person person = new Person();
		person.setName("高德");
		person.setAge((short)11);
		
		Configuration configure = new Configuration().configure();
		SessionFactory sessionFactory = configure.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		session.save(person);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}

	/**
	 * jdbc way
	 */
	private static void run1() {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@10.80.32.180:1521/QMSDEVDB", "qmsdev", "oracle");
			PreparedStatement ps = connection.prepareStatement("select count(*) from user_tables");
			ResultSet rs = ps.executeQuery();
			while (rs.next() ) {
				int count = rs.getInt(1);
				System.out.println(count);
			}
			rs.close();
			ps.close();
			connection.close();
			System.out.println(connection);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
